import ajax, { ListResult,getQuery } from '../ajax';


export function getList(data:any) {
  return ajax.post<ListResult>(`/ControlPanels/Permissions/Query`, data);
}

export function updateEntity(data:any) {
  if (!data.application) {
    data.applicationId = 10;
  }
  
  if(data.permissionId) {
    return ajax.put<any>('/ControlPanels/Permissions', data);
  } else {
    return ajax.post<any>('/ControlPanels/Permissions', data);
  }
}

export function deleteEntity(id:number) {
  let deleteCommand = {"id": id};
  return ajax.put<any>('/ControlPanels/Permissions/Actions/Delete', deleteCommand);
}

export function getAppPerms() {
  let data = getQuery('permissionId');
  return getList(data);
}

export function getRolePerms(roleId: number) {
  return ajax.get<Array<any>>(`/ControlPanels/Roles/${roleId}/Permissions`);
}

export function bindPerm(roleId:number, permissionId:number) {
  let data = {
    roleId,
    permissionId
  }
  return ajax.post(`/ControlPanels/Roles/Actions/Bind/Permission`, data);
}
export function unbindPerm(roleId:number, permissionId:number) {
  let data = {
    roleId,
    permissionId
  }
  return ajax.post(`/ControlPanels/Roles/Actions/Unbind/Permission`, data);
}